package com.pospi.xstore.ui.report;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.params.ProgressParams;
import com.pospi.xstore.R;
import com.pospi.xstore.http.LoadServer;
import com.pospi.xstore.ui.base.BaseActivity;
import com.pospi.xstore.ui.caigou.CgDetailActivity;
import com.pospi.xstore.ui.caigou.CgddActivity;
import com.pospi.xstore.ui.caigou.CgddBeen;
import com.pospi.xstore.util.Api;
import com.pospi.xstore.util.Constant;
import com.pospi.xstore.util.GetData;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import lib.kingja.switchbutton.SwitchMultiButton;

public class StoreReportActivity extends BaseActivity {
    @Bind(R.id.tv_xsmc)
    TextView tv_xsmc;
    @Bind(R.id.tv_xsmd)
    TextView tv_xsmd;
    @Bind(R.id.tv_xsje)
    TextView tv_xsje;

    @Bind(R.id.tv_kcmd)
    TextView tv_kcmd;
    @Bind(R.id.tv_kcje)
    TextView tv_kcje;
    @Bind(R.id.tv_kcsl)
    TextView tv_kcsl;



    @Bind(R.id.tv_cgmc)
    TextView tv_cgmc;
    @Bind(R.id.tv_cgmd)
    TextView tv_cgmd;
    @Bind(R.id.tv_cgje)
    TextView tv_cgje;
    @Bind(R.id.tv_thje)
    TextView tv_thje;

    @Bind(R.id.tv_tbmc)
    TextView tv_tbmc;
    @Bind(R.id.tv_tbmd)
    TextView tv_tbmd;
    @Bind(R.id.tv_trje)
    TextView tv_trje;
    @Bind(R.id.tv_tcje)
    TextView tv_tcje;

    @Bind(R.id.tv_symc)
    TextView tv_symc;
    @Bind(R.id.tv_symd)
    TextView tv_symd;
    @Bind(R.id.tv_syje)
    TextView tv_syje;



    @Bind(R.id.switch_button)
    SwitchMultiButton switch_button;


    private List<CgddBeen> beens = new ArrayList<>();
    private String count;
    private String je;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.mdjy_list_item);
        ButterKnife.bind(this);
        setTitle("门店经营");
        setMenuImage(R.drawable.icon_search_white);
        init();
//        downStore();
    }
    @Override
    public void onMenuClick(View view) {
        super.onMenuClick(view);
        Intent intent = new Intent(this, StoreReportSearchActivity.class);
        startActivity(intent);

    }



    private void init() {

        switch_button.setText("今天","昨天","近三天","近七天","近一月");
        switch_button.setOnSwitchListener(new SwitchMultiButton.OnSwitchListener() {
            @Override
            public void onSwitch(int position, String tabText) {
                switch (position) {
                    case 0:
                        md = "所有门店";
                        startrq = GetData.getAnyTime(0);
                        endrq = GetData.getAnyTime(0);
                        startDown();
                        break;
                    case 1:
                        md = "所有门店";
                        startrq = GetData.getAnyTime(1);
                        endrq = GetData.getAnyTime(1);
                       startDown();
                        break;
                    case 2:
                        md = "所有门店";
                        startrq = GetData.getAnyTime(3);
                        endrq = GetData.getAnyTime(0);
                       startDown();
                        break;
                    case 3:
                        md = "所有门店";
                        startrq = GetData.getAnyTime(7);
                        endrq = GetData.getAnyTime(0);
                        startDown();
                        break;
                    case 4:
                        md = "所有门店";
                        startrq = GetData.getAnyTime(30);
                        endrq = GetData.getAnyTime(0);
                        startDown();
                        break;
                }
            }
        });
        startDown();

    }
    private void startDown(){
        searchxs();
    }

    private String marktId = "";
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        switch_button.clearSelection();
        startrq = intent.getStringExtra("startrq");
        endrq= intent.getStringExtra("endrq");
        marktId = intent.getStringExtra("market");
        md = intent.getStringExtra("marketname");
        searchxs();
    }

    private String rkjjje = "";
    private String tcjjje = "";
    private String syjje = "";
    private String xsjjje = "";
    private String xslsjje = "";

    private String dboutjjje = "";
    private String dbinjjje = "";
    private String pdjjje = "";
    private String psoutjjje = "";
    private String psinjjje = "";
    private String kcsl = "";
    private String kcjjje = "";



    //销售查询
    private void searchxs() {
        showDialog();
        RequestParams params = new RequestParams();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "report.Mobile");
        params.put("fun", "find_markthz");

        JSONObject object = new JSONObject();
        try {
            object.put("startrq", startrq);
            object.put("endrq",endrq);
            object.put("market", marktId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        Log.i("login",params.toString());
        LoadServer.getConnect(getApplicationContext(), Api.LOGIN, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Log.i("report", new String(bytes));
                try {
                    JSONObject jsonObject = new JSONObject(new String(bytes));
                    Log.i("login", jsonObject.getString("errMsg"));
                    JSONObject result = jsonObject.getJSONObject("result");
                    rkjjje = result.getString("rkjjje");
                    tcjjje = result.getString("tcjjje");
                    syjje = result.getString("syjje");
                    xslsjje = result.getString("xslsjje");
                    dboutjjje = result.getString("dboutjjje");

                    dbinjjje = result.getString("dbinjjje");
                    kcjjje = result.getString("kcjjje");
                    kcsl = result.getString("kcsl");



                } catch (JSONException e) {

                    e.printStackTrace();
                }
                Message message = Message.obtain();
                message.what = 1;
                handler.sendMessage(message);
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
            }
        });
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    close();
                    setData();

                    break;
                case 2:

                    break;
                case 3:

                    break;
                case 4:

                    break;
            }

        }
    };
    private String md = "所有门店";

    private void setData() {
        tv_xsje.setText(xslsjje);
        tv_kcje.setText(kcjjje);
        tv_kcsl.setText(kcsl);
        tv_cgje.setText(rkjjje);
        tv_thje.setText(tcjjje);
        tv_trje.setText(dbinjjje);
        tv_tcje.setText(dboutjjje);
        tv_syje.setText(syjje);

        tv_xsmd.setText(md);
        tv_kcmd.setText(md);
        tv_cgmd.setText(md);
        tv_tbmd.setText(md);
        tv_symd.setText(md);

    }

    private void setbill(boolean o){
        if (o) {

        } else {

            beens.clear();
        }

    }


    //下载门店
    private void downStore() {
        RequestParams params = new RequestParams();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getlastmarket");
        Log.i("goods", params.toString());
        LoadServer.getConnect(getApplicationContext(), Api.LOGIN, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Log.i("login", new String(bytes));
                try {
                    JSONObject jsonObject = new JSONObject(new String(bytes));
                    JSONArray result = jsonObject.getJSONArray("result");
                    SPHelper.getInstance().putString(Constant.STORE_LIST, result.toString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
            }
        });

    }


    private String startrq = GetData.getAnyTime(0);
    private String endrq = GetData.getAnyTime(0);


}
